<template>
  <div class="container" :style="setSize()">
    <div class="content">
      <el-card shadow="hover">
        <el-form ref="account" :rules="rules" :model="account" style="margin-top:25px">
          <el-form-item label="账号" prop="userName">
              <el-input v-model="account.userName"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input  type="password" v-model="account.password"></el-input>
          </el-form-item>
          <el-form-item style="text-align: center">
            <el-button type="primary" @click="userLogin">登录</el-button>
            <el-button type="success" @click="userRegister">注册</el-button>
          </el-form-item>
        </el-form> 
      </el-card>
    </div>
  </div>
</template>

<script>
import {register, login} from '@/api/index.js'
export default {
  name:'login',
  data(){
    return {
      account:{},
      rules:{
        userName:[{required:true, message: '请输入用户名', trigger: 'blur'}],
        password:[{required:true, message: '请输入密码', trigger: 'blur'}],
      }
    }
  },
  methods:{
    // 注册
    userRegister(){
      this.$refs.account.validate(valid => {
        if(valid) {
         const params = {
                userName:this.account.userName,
                password:this.account.password
              }
          register(params).then(res => {
            console.log('res',res)
            if(res.data.code == 200) {
              this.$message.success('注册成功！')
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      })
    },
    // 登录
    userLogin(){
      this.$refs.account.validate(valid => {
        if(valid) {
          this.$axios.get('/api/login',{
            params: {
              userName:this.account.userName,
              password:this.account.password
            }
          }).then(res => {
            console.log('res',res)
            if(res.data.code == 200){
              localStorage.setItem('token',res.data.token);
              this.$router.push({path: '/home'})
              this.$message.success('登录成功！')
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      })
      
    }, 
    setSize(){
      return {'height': window.innerHeight + 'px', 'width': window.innerWidth + 'px'}
    },   
  }
}
</script>

<style lang="scss" scoped>
  .container{
    background-image: url(https://s.cn.bing.net/th?id=OHR.GreatRidge_ZH-CN6165605288_1920x1080.jpg&rf=LaDigue_1920x1080.jpg); opacity: 1;
    position: relative;
  }
  .content{
    width: 25vw;
    height: 25vh;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
  }
  .el-input{
    width: 80%;
  }
  
</style>